/*H**************************************************************************
* NAME:         FS_dir.h         
*----------------------------------------------------------------------------
// Program Description:
//
// Header file with function prototypes 
//-----------------------------------------------------------------------------
// Header File Preprocessor Directive
//-----------------------------------------------------------------------------

*****************************************************************************/

#ifndef _FILESYS_H_
#define _FILESYS_H_
//-----------------------------------------------------------------------------
// Structure Prototypes
//-----------------------------------------------------------------------------
typedef union {unsigned int i; unsigned char c[2];} WORD;
typedef struct {
	unsigned cluster_start;
	unsigned long sector_current;
	unsigned sector_direntry;
	unsigned offset_direntry;
	unsigned char attrib;
	unsigned long int pos;
	unsigned long int size;
	unsigned char isopen;
} FILE;

typedef WORD UNI; // Unicode character type

typedef union {
	
	struct {
		unsigned char seq_nr; // Bit 0..4 = seqnr, ascending, start with 1. Bit 6: final part of name
		UNI unicode1_5[5]; // Little endian
		unsigned char attrib; // 0x0F
		unsigned char type; // 0x00
		unsigned char checksum; // int i;BYTE sum=0;for(i=0;i<11;i++) { sum=(sum>>1)+((sum&1)<<7);sum+=name[i]; }
		UNI unicode6_11[6];
		unsigned starting_cluster; // 0x0000
		UNI unicode12_13[2];
	} lfn;
	
	
	struct {
		char name[11];		//	11
		unsigned char attrib;	// 12
		unsigned char reserved;	//Reserved by NT  13
		unsigned char crttimelenth;	 //Time Created, tenths of second portion 14
		WORD createdtime;			    //created time	  //15- 16	   2-3
		WORD createddate;		   //date created	 17-18			   4-5
		WORD lastaccdate;	   // last access date	19-20		   6-7
		WORD fstclushi;		//High word of entry's first cluster number	8-9				//	21-22
		WORD time; // 5/6/5 bits for h/m/2*sec	last updated time	 23-24
		WORD date; // 7/4/5 bits for y-1980/m/d	last updated Date	 25-26
		unsigned starting_cluster; // 0 for empty file -27-28
		//Low word of entry's first cluster number	
		unsigned long int filesize;	//				   28-32
	} sfn;
	
} dir_entry;

typedef struct {
	unsigned long block;
	unsigned offset;
	unsigned char findempty;
	dir_entry* direntry;
} find_info;



#define ATTRIB_READ_ONLY 	0x01
#define ATTRIB_HIDDEN		0x02
#define ATTRIB_SYSTEM		0x04
#define ATTRIB_LABEL		0x08
#define ATTRIB_SUBDIR		0x10
#define ATTRIB_ARCHIVE		0x20

#define SEEK_CUR 0
#define SEEK_END 1
#define SEEK_SET 2

#define FS_NOCARD  0
#define FS_OK	   1
#define FS_ERROR   2
#define FS_NOT_INIT  0

#define MMCFS_INIT    0
#define MMCFS_REFRESH 1

//new constants:
#define	FIRST_CLUSTER	0X02
#define	MAX_FAT16_CLUSTERS	0XFFEF

#define	DELETED	0xE5
#define	NO_DATA	0xFFFF
#define	LAST_CLUSTER_CONSTANT	0xFFFFFFFF
#define	EOC_MARKER_START	0xFFF8
#define	EOC_MARKER_END			0xFFFF

#define	RESERVED_SECTOR_START	0xFFF0
#define	LONG_FILE_ENTRY			0x0F
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

int fopen(FILE* f,char* filename,char* mode);
int fseek(FILE* f,long offset,int origin);
unsigned long int ftell(FILE* f);
int feof(FILE* f);
unsigned fread(FILE* f,unsigned char* buffer,unsigned count);
unsigned fwrite(FILE* f,unsigned char* buffer,unsigned count);
void fclose(FILE* f);
int fexists(char* filename);
int fdelete(char* filename);
unsigned char findfirst(find_info* findinfo,unsigned char empty);
unsigned char findnext(find_info* findinfo);


#define DIRECTORY_EXISTS		1
#define NO_PLACE_FOR_DIRECTORY	2
#define DIRNAME_LENGTH_ERROR	3

#define	FS_FUNC_SUCCESS		1
#define FS_FUNC_FAIL		0
#define	FS_FUNC_CARD_WP		2
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void FileSys_Init();
extern unsigned char chngdir(char* dirname);
extern unsigned char mkdir(char* dir_name);
extern unsigned char rmdir(char* dir_name) ;
extern unsigned char MMCSD_FileSystem_Init(unsigned char fstate);
extern void MMCSD_FileSystem_Refresh(void);

//-----------------------------------------------------------------------------
// Structure Prototypes
//-----------------------------------------------------------------------------

typedef struct 
{ 
	unsigned long block;
	unsigned offset;
}
PREV_SEARCH;


#endif

